package com.cqeec.waimai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqeec.waimai.common.R;
import com.cqeec.waimai.entity.Employee;
import com.cqeec.waimai.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
//import sun.net.httpserver.HttpsServerImpl;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;
//    登录功能
    @PostMapping("login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
//        获取用户输入的密码，并且进行md5加密
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
//        获取用户名这个字段  用户输入的用户名
        wrapper.eq(Employee::getUsername,employee.getUsername());
//        根据用户名进行查询，将查询结果保存
        Employee one = employeeService.getOne(wrapper);
//        System.out.println(one);
        if(one == null){
            return R.error("用户不存在");
        }
        if(!one.getPassword().equals(password)){
            return R.error("密码错误");
        }if(one.getStatus()==0){
            return R.error("账户被禁用");
        }
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);
    }
//    注销功能
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出");
    }
}
